<?php
	class VoteAction extends BaseAction{
	public function index(){
			$voteSubject = M('VoteSubject');
			import("ORG.Util.Page");
			$count = $voteSubject->count();
			$page = new Page($count,c('PAGESIZE'));
			$show = $page->show();
			$data = $voteSubject->limit($page->firstRow.','.$page->listRows)
							->field('zgcms_vote_subject.id,zgcms_vote_subject.title,state,sum(count) count')
							->join('zgcms_vote_item on zgcms_vote_subject.id = zgcms_vote_item.sid')
							->group('zgcms_vote_subject.id')
							->select();
// 			echo $voteSubject->getLastSql();
// 			dump($data);
			$this->assign("list",$data);
			$this->assign('page',$show);
			$this->display();
		}

		//通过投票展示
		public function pass(){
			if(empty($_GET['id'])){
				$this->error('请选择要展示的投票主题！');
				exit();
			}
			$voteSubject = M('VoteSubject');
			$voteSubjectData = $voteSubject->getById($_GET['id']);
			if(!$voteSubjectData){
				$this->error('请确保要展示的投票主题ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 0;
			$voteSubject->where('state = 1')->save($data);
			$data['state'] = 1;
			if($voteSubject->where('id = '.$_GET['id'])->save($data)){
				$this->success('投票展示成功！');
			}else{
				$this->error('投票展示失败！');
			}
		
		}
		
		
		public function add(){
			$this->display();
		}

		public function insert(){
			$voteSubject = D('VoteSubject');
			if($voteSubject->create())
			{
				if($voteSubject->add())
				{
					$this->assign("jumpUrl","__URL__");
					$this->success("添加成功！");
				}else{
					$this->error("添加失败！");
				}
			}else{
				$this->error($voteSubject->getError());
			}
		}

		public function edit()
		{
			if(!empty($_GET['id']))
			{
				$voteSubject = M('VoteSubject');
				$data = $voteSubject->getById($_GET['id']);
				// 				var_dump($data);
				if(null == $data)
					$this->error("请确认要修改的投票主题ID合法！");
				$this->assign('voteSubject',$data);
				$this->display();

			}else{
				$this->error("请选择要修改的投票主题！");
			}
		}

		public function update()
		{
			$voteSubject = D('VoteSubject');
			if($voteSubject->create()){
				if($voteSubject->save()){
					$this->success("更新成功！");
				}else{
					$this->error("因为数据没变化或更新失败！");
				}
			}else{
				$this->error($voteSubject->getError());
			}
		}
		
		public function delete()
		{
			if(!empty($_GET['id']))
			{
				$voteSubject = M('VoteSubject');
				$voteItem = M('VoteItem');
				$voteItem->where('sid = '.$_GET['id'])->delete();
				if($voteSubject->delete($_GET['id'])){
					$this->success("删除投票主题成功!");
				}else{
					$this->error("删除投票主题失败!");
				}
			}else{
				$this->error("请选择要删除的投票主题！");
			}
		}

		//显示投票主题下的选项
		public function showItem(){
			if(empty($_GET['sid'])){
				$this->error('请选择要查看的投票主题！');
				exit();
			}
			$voteItem = M('VoteItem');
			import("ORG.Util.Page");
			$count = $voteItem->where('sid = '.$_GET['sid'])->count();
			$page = new Page($count,c('PAGESIZE'));
			$show = $page->show();
			$data = $voteItem->limit($page->firstRow.','.$page->listRows)
						->where('sid = '.$_GET['sid'])
						->field(true)
						->select();
			$voteSubject = M('VoteSubject');
			$voteData = $voteSubject->field('title,id')
									->where('id = '.$_GET['sid'])
									->find();
			$this->assign('sid',$voteData['id']);
			$this->assign('voteSub',$voteData);
			$this->assign("list",$data);
			$this->assign('page',$show);
			$this->display();
		}
		
		//显示增加投票选项页面
		public function addItem(){
			if(empty($_GET['sid'])){
				$this->error('请选择要增加选项的投票主题！');
				exit();
			}
			$voteSub = M('VoteSubject');
			$voteData = $voteSub->getById($_GET['sid']);
			if(!$voteData){
				$this->error('不存在该投票主题！');
				exit();
			}
			$this->assign('subTitle',$voteData['title']);
			$this->assign('sid',$_GET['sid']);
			$this->display();
		}
		
		//增加投票选项
		public function insertItem(){
			$voteItem = D('VoteItem');
			if($voteItem->create())
			{
				if($voteItem->add())
				{
					$this->assign("jumpUrl","__URL__/showItem/sid/".$_POST['sid']);
					$this->success("添加成功！");
				}else{
					$this->error("添加失败！");
				}
			}else{
				$this->error($voteItem->getError());
			}
		}
		
		//显示修改投票选项页面
		public function editItem(){
			if(!empty($_GET['id']))
			{
				$voteItem = M('VoteItem');
				$data = $voteItem->getById($_GET['id']);
				if(null == $data)
					$this->error("请确认要修改的选项ID合法！");
				$this->assign('voteSubject',$data);
				$this->assign('sid',$_GET['sid']);
				$this->display();
			
			}else{
				$this->error("请选择要修改的选项！");
			}
		}
		
		//修改选项
		public function updateItem(){
			$voteItem = D('VoteItem');
			if($data = $voteItem->create()){
				if($voteItem->save()){
					$this->success("更新成功！");
				}else{
					$this->error("因为数据没变化或更新失败！");
				}
			}else{
				$this->error($voteItem->getError());
			}
		}
		
		//删除选项
		public function deleteItem(){
			if(!empty($_GET['id']))
			{
				$voteItem = M('VoteItem');
				if($voteItem->delete($_GET['id'])){
					$this->success("删除投票选项成功!");
				}else{
					$this->error("删除投票选项失败!");
				}
			}else{
				$this->error("请选择要删除的投票选项！");
			}
		}
		
		
	}
?>